package com.example.myapplication;

import android.Manifest;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.example.myapplication.fragment.AnimationFragment;
import com.example.myapplication.fragment.FragmentAdapter;
import com.example.myapplication.fragment.WordFragment;
import com.example.myapplication.service.MusicService;

import java.util.ArrayList;
import java.util.List;


public class ListViewActivity extends AppCompatActivity implements View.OnClickListener {

    private String  TAG = ListViewActivity.class.toString();
    private AnimationFragment animationFragment;
    private WordFragment wordFragment;
    private ViewPager viewPager;
    private Button btn1,btn2;

    private String[] PERMISSION = new String[] {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    private boolean checkPermission() {
        for (String str: PERMISSION) {
            int result = ContextCompat.checkSelfPermission( this, str);
            if(result != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(checkPermission() == false) {
            ActivityCompat.requestPermissions(this, PERMISSION, 0);
        }

        setContentView(R.layout.activity_list_layout);

        Intent intent = getIntent();
        String userName = intent.getStringExtra(Const.USER_NAME_KEY);
        TextView userNameTv = findViewById(R.id.user_name);
        userNameTv.setText(userName);


        animationFragment  =  new AnimationFragment();
        wordFragment = new WordFragment();
        List<Fragment> fragmentList = new ArrayList<>();

        fragmentList.add(animationFragment);
        fragmentList.add(wordFragment);

        viewPager = findViewById(R.id.fragment);
        FragmentAdapter adapter = new FragmentAdapter(
                getSupportFragmentManager(),
                fragmentList);

        viewPager.setAdapter(adapter);



        Intent resultIntent = new Intent();
        resultIntent.putExtra(Const.NUMBER_KEY,  91011);
        setResult(6666,  resultIntent  );

        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);

        btn1.setBackgroundColor(Color.parseColor("#1FCFE7"));

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int p) {
                btn1.setBackgroundColor(Color.parseColor(p == 0 ? "#1FCFE7" : "#D5D5D5"));
                btn2.setBackgroundColor(Color.parseColor(p == 1 ? "#1FCFE7" : "#D5D5D5"));
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        Intent i = new Intent(ListViewActivity.this, MusicService.class);

        bindService(i, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                wordFragment
                        .setMusicController(
                                (MusicService.MusicController)service);
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        }, BIND_AUTO_CREATE);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                viewPager.setCurrentItem(0);
                btn1.setBackgroundColor(Color.parseColor("#1FCFE7"));
                btn2.setBackgroundColor(Color.parseColor("#D5D5D5"));
                break;
            case R.id.btn2:
                viewPager.setCurrentItem(1);
                btn2.setBackgroundColor(Color.parseColor("#1FCFE7"));
                btn1.setBackgroundColor(Color.parseColor("#D5D5D5"));
                break;
        }
    }
}
